package com.wqd.freemarker.controller;

import com.wqd.freemarker.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;

@Controller
@RequestMapping("/hello")
public class HelloController {

    @GetMapping("/basic")
    public String freeMarkerTest(Model model){
        model.addAttribute("name","yang");

        Student student = new Student();
        student.setName("yang");
        student.setAge(18);
        model.addAttribute(student);
        return "01-basic";
    }

    @GetMapping("/list")
    public String list(Model model){
        Student stu1 = new Student();
        Student stu2 = new Student();

        stu1.setName("小红");
        stu1.setMoney(100f);
        stu1.setAge(20);

        stu2.setName("诸葛亮");
        stu2.setMoney(10000f);
        stu2.setAge(200);

        List<Student> list = new ArrayList<>();
        Collections.addAll(list,stu1,stu2);
        model.addAttribute("stus",list);

        Map map = new HashMap();
        map.put("stu1",stu1);
        map.put("stu2",stu2);
        model.addAttribute("stuMap",map);

        return "02-list";
    }

    @GetMapping("/operation")
    public String testOperation(Model model) {
        //构建 Date 数据
        Date now = new Date();
        model.addAttribute("date1", now);
        model.addAttribute("date2", now);

        Student stus = new Student();
        stus.setName("yang");
        stus.setAge(18);
        model.addAttribute(stus);

        return "03-inner";
    }

    @GetMapping("innerFunc")
    public String testInnerFunc(Model model) {
        //1.1 小强对象模型数据
        Student stu1 = new Student();
        stu1.setName("小强");
        stu1.setAge(18);
        stu1.setMoney(1000.86f);
        stu1.setBirthday(new Date());
        //1.2 小红对象模型数据
        Student stu2 = new Student();
        stu2.setName("小红");
        stu2.setMoney(200.1f);
        stu2.setAge(19);
        //1.3 将两个对象模型数据存放到List集合中
        List<Student> stus = new ArrayList<>();
        stus.add(stu1);
        stus.add(stu2);
        model.addAttribute("stus", stus);
        // 2.1 添加日期
        Date date = new Date();
        model.addAttribute("today", date);
        // 3.1 添加数值
        model.addAttribute("point", 102920122);
        return "04-innerFunc";
    }
}
